<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Auth_Action extends Action
{
	// current action name
	var $action = 'auth';

	// avaliable methods
	var $methods = array('view', 'logout');

	// default method
	var $default = 'view';

	public function view()
	{
		if ($this->curUser->info['user_id'] > 0)
		{
			Engine::showError(3);
		}

		$err = null;

		if (get_request('auth', 'post') == 1)
		{
			Engine::secretKey(true);
			if (!get_request('login', 'post') || !get_request('password', 'post'))
			{
				$err = lang::instance()->get('auth', 'empty_fields');
			}
			elseif (!$this->UserModel->auth())
			{
				$err = lang::instance()->get('auth', 'wrong_data');
			}
			else
			{
				redirect(make_link());
			}
		}

		tpl::instance()->assign('err', $err);
		tpl::instance()->show('auth');
	}

	public function logout()
	{
		if (Router::param(0) == $this->curUser->logout_hash)
		{
			$this->UserModel->logout();
		}
		else
		{
			Engine::showError(4);
		}
	}
}